/*
 * Copyright (c) 2014. qudapeng (qu_dapeng@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhs.gccs.utils;

import android.text.TextUtils;
import android.util.Log;

public class LogUtils
{

    public static String customTagPrefix = "";

    private LogUtils()
    {
    }

    public static boolean DEBUG = true;
    public static boolean allowD = DEBUG;
    public static boolean allowE = DEBUG;
    public static boolean allowI = DEBUG;
    public static boolean allowV = DEBUG;
    public static boolean allowW = DEBUG;
    public static boolean allowWtf = DEBUG;

    private static String generateTag(StackTraceElement caller)
    {
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
        return tag;
    }

    public static CustomLogger customLogger;

    public interface CustomLogger
    {
        void d(String tag, String content);

        void d(String tag, String content, Throwable tr);

        void e(String tag, String content);

        void e(String tag, String content, Throwable tr);

        void i(String tag, String content);

        void i(String tag, String content, Throwable tr);

        void v(String tag, String content);

        void v(String tag, String content, Throwable tr);

        void w(String tag, String content);

        void w(String tag, String content, Throwable tr);

        void w(String tag, Throwable tr);

        void wtf(String tag, String content);

        void wtf(String tag, String content, Throwable tr);

        void wtf(String tag, Throwable tr);
    }

    public static void d(String content)
    {
        if (!allowD)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.d(tag, content);
        }
        else
        {
            Log.d(tag, content);
        }
    }

    public static void d(String content, Throwable tr)
    {
        if (!allowD)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.d(tag, content, tr);
        }
        else
        {
            Log.d(tag, content, tr);
        }
    }

    public static void e(String content)
    {
        if (!allowE)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.e(tag, content);
        }
        else
        {
            int size = 3 * 1024;
            int i = 0;
            if(content.length() > 2000) {
                while (content.length() > size) {
                    String logContent = content.substring(0, size);
                    content = content.replace(logContent, "");
                    Log.e(tag + "@" + i++, logContent);
                }
                Log.e(tag + "@" + i, content);
            }
            else {
                Log.e(tag, content);
            }
        }
    }

    public static void e(String content, Throwable tr)
    {
        if (!allowE)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.e(tag, content, tr);
        }
        else
        {
            Log.e(tag, content, tr);
        }
    }

    public static void i(String content)
    {
        if (!allowI)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.i(tag, content);
        }
        else
        {
            Log.i(tag, content);
        }
    }

    public static void i(String content, Throwable tr)
    {
        if (!allowI)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.i(tag, content, tr);
        }
        else
        {
            Log.i(tag, content, tr);
        }
    }

    public static void v(String content)
    {
        if (!allowV)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.v(tag, content);
        }
        else
        {
            Log.v(tag, content);
        }
    }

    public static void v(String content, Throwable tr)
    {
        if (!allowV)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.v(tag, content, tr);
        }
        else
        {
            Log.v(tag, content, tr);
        }
    }

    public static void w(String content)
    {
        if (!allowW)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.w(tag, content);
        }
        else
        {
            Log.w(tag, content);
        }
    }

    public static void w(String content, Throwable tr)
    {
        if (!allowW)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.w(tag, content, tr);
        }
        else
        {
            Log.w(tag, content, tr);
        }
    }

    public static void w(Throwable tr)
    {
        if (!allowW)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.w(tag, tr);
        }
        else
        {
            Log.w(tag, tr);
        }
    }

    public static void wtf(String content)
    {
        if (!allowWtf)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.wtf(tag, content);
        }
        else
        {
            Log.wtf(tag, content);
        }
    }

    public static void wtf(String content, Throwable tr)
    {
        if (!allowWtf)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.wtf(tag, content, tr);
        }
        else
        {
            Log.wtf(tag, content, tr);
        }
    }

    public static void wtf(Throwable tr)
    {
        if (!allowWtf)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        if (customLogger != null)
        {
            customLogger.wtf(tag, tr);
        }
        else
        {
            Log.wtf(tag, tr);
        }
    }

    public static StackTraceElement getCallerStackTraceElement() {
        return Thread.currentThread().getStackTrace()[4];
    }

}
